﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hired
{
    public static class DBAccount
    {
        public static object RetrieveAccountByID(string id)
        {
            string accountType = RetrieveAccountTypeByID(id);
            switch (accountType)
            {
                case "AdminStaffAccount":
                    return DBAdminStaffAccount.retrieveAdminStaffAccountByID(id);
                case "CompanyAccount":
                    return DBCompanyAccount.RetrieveCompanyAccount(id, false);
                case "EmployerPassport":
                    return DBEmployerPassport.RetrieveEmployerPassport(id);
                case "JobseekerAccount":
                    return DBJobseekerAccount.retrieveJobseekerAccount(id, false);
                default:
                    return "Not an account";
            }
        }


        public static object RetrieveAccountByUsername(string username)
        {
            object account = null;
            account = DBAdminStaffAccount.retrieveAdminStaffAccountByUsername(username);
            if (account != null)
            {
                return account;
            }
            account = DBCompanyAccount.RetrieveCompanyAccountByUsername(username, false);
            if (account != null)
            {
                return account;
            }
            account = DBEmployerPassport.RetrieveEmployerPassportByUsername(username);
            if (account != null)
            {
                return account;
            }
            account = DBJobseekerAccount.retrieveJobseekerAccountbyUsername(username, false);
            return account;
        }

        public static object RetrieveAccountByEmail(string email)
        {
            object account = null;
            account = DBAdminStaffAccount.retrieveAdminStaffAccountByEmail(email);
            if (account == null)
            {
                account = DBJobseekerAccount.RetrieveJobseekerAccountByEmail(email);
                if (account == null)
                {
                    account = DBEmployerPassport.RetrieveEmployerPassportByEmail(email);
                    if (account == null)
                    {
                        account = DBCompanyAccount.RetrieveCompanyAccountByEmail(email);
                    }
                }
            }

            return account;
        }

        public static string RetrieveAccountTypeByAccount(object account)
        {
            if (account is AdminStaffAccount)
            {
                return "AdminStaffAccount";
            }
            else if (account is CompanyAccount)
            {
                return "CompanyAccount";
            }
            else if (account is EmployerPassport)
            {
                return "EmployerPassport";
            }
            else if (account is JobseekerAccount)
            {
                return "JobseekerAccount";
            }
            else
            {
                return "Not an account";
            }
        }


        public static string RetrieveAccountTypeByID(string id)
        {
            if (id[0] == 'A')
            {
                return "AdminStaffAccount";
            }
            else if (id[0] == 'C')
            {
                return "CompanyAccount";
            }
            else if (id.Substring(0, 2) == "EP")
            {
                return "EmployerPassport";
            }
            else if (id.Substring(0, 2) == "Js")
            {
                return "JobseekerAccount";
            }
            else
            {
                return "Not an account";
            }
        }

        public static string RetrieveAccountTypeByUsername(string username)
        {
            if (DBAdminStaffAccount.retrieveAdminStaffAccountByUsername(username) != null)
            {
                return "AdminStaffAccount";
            }
            else if (DBCompanyAccount.RetrieveCompanyAccountByUsername(username, false) != null)
            {
                return "CompanyAccount";
            }
            else if (DBEmployerPassport.RetrieveEmployerPassportByUsername(username) != null)
            {
                return "EmployerPassport";
            }
            else if (DBJobseekerAccount.retrieveJobseekerAccountbyUsername(username, false) != null)
            {
                return "JobseekerAccount";
            }
            else
            {
                return "Not an account";
            }
        }

        public static bool ActivateAccount(string username, string activationID)
        {
            if (Encryption.verify(username, activationID))
            {
                string accountType = RetrieveAccountTypeByUsername(username);

                using (hireddbEntities db = new hireddbEntities())
                {
                    if (accountType == "CompanyAccount")
                    {
                        CompanyAccount ca = DBCompanyAccount.RetrieveCompanyAccountByUsername(username, false);
                        db.Attach(ca);
                        ca.Activated = true;
                        db.SaveChanges();
                        return true;
                    }
                    else if (accountType == "EmployerPassport")
                    {
                        EmployerPassport ep = DBEmployerPassport.RetrieveEmployerPassportByUsername(username);
                        db.Attach(ep);
                        ep.Activated = true;
                        db.SaveChanges();
                        return true;

                    }
                    else if (accountType == "JobseekerAccount")
                    {
                        JobseekerAccount jsa = DBJobseekerAccount.retrieveJobseekerAccountbyUsername(username, false);
                        db.Attach(jsa);
                        jsa.Activated = true;
                        db.SaveChanges();
                        return true;
                    }
                }
            }
            return false;
        }

        public static string RetrieveAccountIDByUsername(string username)
        {
            string accountType = RetrieveAccountTypeByUsername(username);
            
            switch (accountType)
            {
                //Check AdminStaffAccount
                case "AdminStaffAccount":
                    using (hireddbEntities db = new hireddbEntities())
                    {
                        var account =
                            (from a in db.AdminStaffAccounts
                             where a.Username == username
                             select a).SingleOrDefault();
                        return account.ASAccountID;
                    }
                //Check Company Account
                case "CompanyAccount":
                    using (hireddbEntities db = new hireddbEntities())
                    {
                        var account =
                            (from ca in db.CompanyAccounts
                             where ca.Username == username
                             select ca).SingleOrDefault();
                        return account.CAccountID;
                    }
                //Check EmployerPassport
                case "EmployerPassport":
                    using (hireddbEntities db = new hireddbEntities())
                    {
                        var account =
                            (from ep in db.EmployerPassports
                             where ep.Username == username
                             select ep).SingleOrDefault();
                        return account.EPassportID;
                    }
                //Check JobseekerAccount
                case "JobseekerAccount":
                    using (hireddbEntities db = new hireddbEntities())
                    {
                        var account =
                            (from ja in db.JobseekerAccounts
                             where ja.Username == username
                             select ja).SingleOrDefault();
                        return account.JAccountID;
                    }
                default:
                    return "Not an account.";
            }
        }
    }
}